package com.xjscrm.console.service.customer.list.impl;

import com.github.pagehelper.PageHelper;
import com.xjscrm.common.constant.ResponseMsg;
import com.xjscrm.common.entity.WwEnterpriseBlacklist;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.common.result.ResponseCode;
import com.xjscrm.common.utils.OrderByUtil;
import com.xjscrm.console.mapper.EnterpriseBalckListMapper;
import com.xjscrm.console.param.list.EnterpriseBlackListQuery;
import com.xjscrm.console.param.list.WwEnterpriseBlackDto;
import com.xjscrm.console.service.customer.list.EnterpriseBalckListService;
import com.xjscrm.console.vo.MyPageInfo;
import com.xjscrm.console.vo.WwCustomerBlackVo;
import com.xjscrm.console.vo.WwEnterpriseBlacklistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EnterpriseBalckListServiceImpl implements EnterpriseBalckListService {

    @Autowired
    private EnterpriseBalckListMapper enterpriseBalckListMapper;

    @Override
    public void addEnterpriseBlackList(WwEnterpriseBlackDto wwEnterpriseBlackDto) {
        if(wwEnterpriseBlackDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(wwEnterpriseBlackDto.getOperatorType()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        if(wwEnterpriseBlackDto.getNames()==null||wwEnterpriseBlackDto.getNames().isEmpty()){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        //主账号
        if(wwEnterpriseBlackDto.getOperatorType()==0){
            wwEnterpriseBlackDto.setOperatorId(wwEnterpriseBlackDto.getMerId());
        }
        //子账号
        if(wwEnterpriseBlackDto.getOperatorType()==1){
            wwEnterpriseBlackDto.setOperatorId(wwEnterpriseBlackDto.getChildId());
        }

        for (String name:wwEnterpriseBlackDto.getNames()) {
            try {
                WwEnterpriseBlacklist wwEnterpriseBlacklist=new WwEnterpriseBlacklist();
                wwEnterpriseBlacklist.setMerId(wwEnterpriseBlackDto.getMerId());
                wwEnterpriseBlacklist.setName(name);
                wwEnterpriseBlacklist.setOperatorId(wwEnterpriseBlackDto.getOperatorId());
                wwEnterpriseBlacklist.setOperatorType(wwEnterpriseBlackDto.getOperatorType());
                enterpriseBalckListMapper.addWwEnterpriseBlacklist(wwEnterpriseBlacklist);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public Object getEnterpriseBlackList(EnterpriseBlackListQuery enterpriseBlackListQuery) {
        if(enterpriseBlackListQuery.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        String orderBy = OrderByUtil.getOrderBy(enterpriseBlackListQuery.getOrderByField(), enterpriseBlackListQuery.isAsc());
        PageHelper.startPage(enterpriseBlackListQuery.getCurrent(),enterpriseBlackListQuery.getSize(),orderBy);
        List<WwEnterpriseBlacklistVo> wwEnterpriseBlacklistVoList=enterpriseBalckListMapper.getEnterpriseBlackList(enterpriseBlackListQuery);
        if(wwEnterpriseBlacklistVoList==null){
            return new MyPageInfo<>(null);
        }
        MyPageInfo<WwEnterpriseBlacklistVo> pageInfo = new MyPageInfo<>(wwEnterpriseBlacklistVoList);
        return pageInfo;
    }

    @Override
    public void deleEnterpriseBlackList(WwEnterpriseBlackDto wwEnterpriseBlackDto) {
        if(wwEnterpriseBlackDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.MERID_NULL);
        }
        if(wwEnterpriseBlackDto.getId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.ID_NULL);
        }
        enterpriseBalckListMapper.deleById(wwEnterpriseBlackDto.getId(),wwEnterpriseBlackDto.getMerId());
    }
}
